package org.varscape.core.reader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;

import org.varscape.core.util.Tokenizer;

public class VCFIteratorReader extends AbstractVCFReader implements IIteratorReader {

	private Scanner scanner;
	private ArrayList<String> strArray;
	private boolean headerDone;
	private String nextLine = null;	// this stores the next line before calling "next()"
	
	//Constructor
	VCFIteratorReader(InputStream inStream) {
		scanner = new Scanner(inStream);
		strArray = new ArrayList<String>();
		headerDone = false;
	}
	
	// It skips all comment lines
	@Override
	public boolean hasNext() {
		if (nextLine != null) {
			return true;
		}
		while (scanner.hasNextLine()) {
			nextLine = scanner.nextLine();
			if (nextLine.startsWith("#")) {
				strArray.add(nextLine);
			} else { 
				headerDone = true;
				return true;
			}
		}
		return false;
	}

	// After calling next(), currentLine and currentLineTokens will be updated.
	@Override
	public void next() {
		currentLine = nextLine;
		currentLineTokens = Tokenizer.tabTokenize(currentLine);
		nextLine = null;
	}

	@Override
	public void close() {
		scanner.close();
	}

	// P.s. Also used by Tabix reader.
	@Override
	public String[] getHeaders() {
		if (!headerDone) {
			hasNext();
		}
		return strArray.toArray(new String[0]);
	}

}
